﻿
Imports DotNetNuke
Imports DotNetNuke.Entities.Users

Imports DotNetNuke.Common
Imports DotNetNuke.Common.Utilities
Imports System.Web.UI.WebControls

Imports DotNetNuke.Entities.Modules
Imports System.Web.UI
Imports DotNetNuke.Entities.Profile
Imports Telerik.Web.UI
Imports System
Imports DotNetNuke.Framework
Imports DotNetNuke.Services.Localization
Imports System.Data
Imports System.Collections.Generic


Namespace dnnWerk.Modules.Users
    Public Class UserModuleBase
        Inherits PortalModuleBase


#Region "Enumerations"

        Protected Enum UsernameUpdateMode
            UserSelect 'user selects the username himself
            FirstLetterLastname 'username autogenerated, username = f.lastname
            FirstnameLastname 'username autogenerated, username = firstname.lastname
            Lastname 'username autogenerated, lastname = username
            Email 'username autogenerated, email = username
        End Enum

        Protected Enum DisplaynameUpdateMode
            UserSelect 'user selects the displayname himself
            FirstLetterLastname 'autogenerated, (F.Lastname)
            FirstnameLastname 'autogenerated (Firstname Lastname)
            Lastname 'username autogenerated, (Lastname)
            Email 'autogenerated, (email address)
        End Enum


#End Region

#Region "Registration Control Settings"

        Protected ReadOnly Property AdditionalControls() As String()
            Get
                Dim strControls As String() = New String() {}

                If Not Settings("AdditionalControls") Is Nothing Then
                    Try
                        strControls = Convert.ToString(Settings("AdditionalControls")).Split(Char.Parse(";"))
                    Catch
                    End Try
                End If

                Return strControls
            End Get
        End Property

        Protected ReadOnly Property ExportFieldHeaders() As String()
            Get

                Dim strPropArray As String() = New String() {}
                Dim strProps As String = "User_UserId,User_Username,User_Firstname,User_Lastname,User_Email,User_CreatedDate,User_LastLoginDate,"

                Dim props As ProfilePropertyDefinitionCollection = ProfileController.GetPropertyDefinitionsByPortal(PortalSettings.PortalId)
                For Each prop As ProfilePropertyDefinition In props
                    If Not String.IsNullOrEmpty(Localization.GetString(prop.PropertyName, ProfileResourcefile)) Then
                        strProps += Localization.GetString(prop.PropertyName, ProfileResourcefile) & ","
                    Else
                        strProps += prop.PropertyName & ","
                    End If
                Next

                If Not Settings("ExportFields") Is Nothing Then
                    If Not String.IsNullOrEmpty(CType(Settings("ExportFields"), String)) Then
                        strProps = ""
                        Try

                            Dim strValue As String = Convert.ToString(Settings("ExportFields"))

                            For Each sValue As String In strValue.Split(Char.Parse(","))
                                If sValue.Trim.Length > 0 Then
                                    If Not String.IsNullOrEmpty(Localization.GetString("ProfileProperties_" & sValue & ".Text", ProfileResourcefile)) Then
                                        strProps += Localization.GetString("ProfileProperties_" & sValue & ".Text", ProfileResourcefile) & ","
                                    Else
                                        strProps += sValue & ","
                                    End If
                                End If
                            Next

                        Catch
                        End Try

                    End If
                  
                End If

                Return strProps.Split(Char.Parse(","))


            End Get
        End Property

        Protected ReadOnly Property ExportFields() As String()
            Get

                Dim strPropArray As String() = New String() {}
                Dim strProps As String = "User_UserId,User_Username,User_Firstname,User_Lastname,User_Email,User_CreatedDate,User_LastLoginDate,"

                Dim props As ProfilePropertyDefinitionCollection = ProfileController.GetPropertyDefinitionsByPortal(PortalSettings.PortalId)
                For Each prop As ProfilePropertyDefinition In props
                    strProps += prop.PropertyName & ","
                Next

                If Not Settings("ExportFields") Is Nothing Then
                    Try
                        Dim strValue As String = Convert.ToString(Settings("ExportFields"))
                        If strValue.Trim.Length > 0 Then
                            strPropArray = strValue.Split(Char.Parse(","))
                        End If
                    Catch
                    End Try
                End If

                If strPropArray.Length > 0 Then
                    Return strPropArray
                End If

                Return strProps.Split(Char.Parse(","))

            End Get
        End Property

        Protected ReadOnly Property ShowUserDetailTabs() As String()
            Get
                Dim strControls As String() = New String() {"Account", "Password", "Profile", "Roles", "Email"}

                If Not Settings("ShowUserDetailTabs") Is Nothing Then
                    Try
                        Dim strValue As String = Convert.ToString(Settings("ShowUserDetailTabs"))
                        If strValue.Length > 0 Then
                            strControls = strValue.Split(Char.Parse(","))
                        End If
                    Catch
                    End Try
                End If

                Return strControls
            End Get
        End Property

        Protected ReadOnly Property CanRunReports() As Boolean
            Get
                If Not Settings("CanRunReports") Is Nothing Then
                    Return CType(Settings("CanRunReports"), Boolean)
                End If
                Return True
            End Get
        End Property

        Protected ReadOnly Property BrowseRoles() As String()
            Get
                If Not Settings("BrowseRoles") Is Nothing Then
                    Return CType(Settings("BrowseRoles"), String).Split(Char.Parse(";"))
                End If
                Return Nothing
            End Get
        End Property

        Protected ReadOnly Property PreSelectRole() As Integer
            Get
                If Not Settings("PreSelectRole") Is Nothing Then
                    Return CType(Settings("PreSelectRole"), Integer)
                End If
                Return Null.NullInteger
            End Get
        End Property

        Protected ReadOnly Property AllowExport() As Boolean
            Get
                If Not Settings("AllowExport") Is Nothing Then
                    Return CType(Settings("AllowExport"), Boolean)
                End If
                Return True
            End Get
        End Property

        Protected ReadOnly Property AllowCreate() As Boolean
            Get
                If Not Settings("AllowCreate") Is Nothing Then
                    Return CType(Settings("AllowCreate"), Boolean)
                End If
                Return False
            End Get
        End Property

        Protected ReadOnly Property AllowHardDelete() As Boolean
            Get
                If Not Settings("AllowHardDelete") Is Nothing Then
                    Return CType(Settings("AllowHardDelete"), Boolean)
                End If
                Return False
            End Get
        End Property

        Protected ReadOnly Property AllowDelete() As Boolean
            Get
                If Not Settings("AllowDelete") Is Nothing Then
                    Return CType(Settings("AllowDelete"), Boolean)
                End If
                Return False
            End Get
        End Property

        Protected ReadOnly Property UsernameMode() As UsernameUpdateMode
            Get
                If Not Settings("ShowUserName") Is Nothing Then
                    Select Case CType(Settings("ShowUserName"), String)
                        Case "EMAIL"
                            Return UsernameUpdateMode.Email
                        Case "FIRSTLETTER.LASTNAME"
                            Return UsernameUpdateMode.FirstLetterLastname
                        Case "FIRSTNAME.LASTNAME"
                            Return UsernameUpdateMode.FirstnameLastname
                        Case "LASTNAME"
                            Return UsernameUpdateMode.Lastname
                        Case Else
                            Return UsernameUpdateMode.UserSelect
                    End Select
                End If
                Return UsernameUpdateMode.UserSelect
            End Get
        End Property

        Protected ReadOnly Property DisplaynameMode() As DisplaynameUpdateMode
            Get
                If Not Settings("ShowDisplayName") Is Nothing Then
                    Select Case CType(Settings("ShowDisplayName"), String)
                        Case "EMAIL"
                            Return DisplaynameUpdateMode.Email
                        Case "FIRSTLETTER.LASTNAME"
                            Return DisplaynameUpdateMode.FirstLetterLastname
                        Case "FIRSTNAME.LASTNAME"
                            Return DisplaynameUpdateMode.FirstnameLastname
                        Case "LASTNAME"
                            Return DisplaynameUpdateMode.Lastname
                        Case Else
                            Return DisplaynameUpdateMode.UserSelect
                    End Select
                End If
                Return DisplaynameUpdateMode.UserSelect
            End Get
        End Property

        Protected ReadOnly Property RedirectAfterVerify() As String
            Get
                If Not Settings("RedirectAfterVerify") Is Nothing Then
                    If IsNumeric(CType(Settings("RedirectAfterVerify"), String)) Then
                        'must be a tab
                        Dim tc As New TabController
                        Dim t As DotNetNuke.Entities.Tabs.TabInfo = tc.GetTab(CType(Settings("RedirectAfterVerify"), Integer), PortalId, False)
                        If Not t Is Nothing Then
                            Return t.FullUrl
                        End If
                    Else
                        Return CType(Settings("RedirectAfterVerify"), String)
                    End If
                End If
                Return Null.NullString
            End Get
        End Property

        Protected ReadOnly Property RedirectAfterSubmit() As String
            Get
                If Not Settings("RedirectAfterSubmit") Is Nothing Then
                    If IsNumeric(CType(Settings("RedirectAfterSubmit"), String)) Then
                        'must be a tab
                        Dim tc As New TabController
                        Dim t As DotNetNuke.Entities.Tabs.TabInfo = tc.GetTab(CType(Settings("RedirectAfterSubmit"), Integer), PortalId, False)
                        If Not t Is Nothing Then
                            Return t.FullUrl
                        End If
                    Else
                        Return CType(Settings("RedirectAfterSubmit"), String)
                    End If
                End If
                Return Null.NullString
            End Get
        End Property

        Protected ReadOnly Property NotifyAddress() As String
            Get
                If Not Settings("NotifyAddress") Is Nothing Then
                    Return CType(Settings("NotifyAddress"), String)
                End If
                Return Null.NullString
            End Get
        End Property

        Protected ReadOnly Property NotifyUser() As Boolean
            Get
                If Not Settings("NotifyUser") Is Nothing Then
                    Return CType(Settings("NotifyUser"), Boolean)
                End If
                Return False
            End Get
        End Property

        Protected ReadOnly Property IncludeHeaders() As Boolean
            Get
                If Not Settings("IncludeHeaders") Is Nothing Then
                    Return CType(Settings("IncludeHeaders"), Boolean)
                End If
                Return True
            End Get
        End Property

        Protected ReadOnly Property AddToRoleOnSubmit() As Integer
            Get
                If Not Settings("AddToRoleOnSubmit") Is Nothing Then
                    Try
                        Return CType(Settings("AddToRoleOnSubmit"), Integer)
                    Catch ex As Exception
                    End Try
                End If
                Return Null.NullInteger
            End Get
        End Property

        Protected ReadOnly Property RemoveFromRoleOnSubmit() As Integer
            Get
                If Not Settings("RemoveFromRoleOnSubmit") Is Nothing Then
                    Try
                        Return CType(Settings("RemoveFromRoleOnSubmit"), Integer)
                    Catch ex As Exception
                    End Try
                End If
                Return Null.NullInteger
            End Get
        End Property

        Protected ReadOnly Property FilterByRole() As String
            Get
                If Not Settings("FilterByRole") Is Nothing Then
                    Return CType(Settings("FilterByRole"), String)
                End If
                Return Null.NullString
            End Get
        End Property

        Protected ReadOnly Property PreloadList() As Boolean
            Get
                If Not Settings("PreloadList") Is Nothing Then
                    Return CType(Settings("PreloadList"), Boolean)
                End If
                Return False
            End Get
        End Property

        Protected ReadOnly Property FilterSelectByRole() As Integer
            Get
                If Not Settings("FilterSelectByRole") Is Nothing Then
                    Return CType(Settings("FilterSelectByRole"), Integer)
                End If
                Return Null.NullInteger
            End Get
        End Property

        Public ReadOnly Property ControlToLoad() As String
            Get
                If CType(Settings("ControlToLoad"), String) <> "" Then
                    Return CType(Settings("ControlToLoad"), String)
                End If
                Return ""
            End Get
        End Property

        Public ReadOnly Property UsermanagementTab() As Integer
            Get
                If CType(Settings("UsermanagementTab"), String) <> "" Then
                    Return CType(Settings("UsermanagementTab"), Integer)
                End If
                Return PortalSettings.HomeTabId
            End Get
        End Property

        Public ReadOnly Property UserManagementTemplates() As List(Of String)
            Get
                Dim lst As New List(Of String)
                lst.Add("Email.Account.Created")
                lst.Add("Email.Account.Data")
                lst.Add("Email.AddedToRole")
                lst.Add("Email.Password.Reset")
                lst.Add("User.Create")
                lst.Add("User.Edit")
                lst.Add("User.Profile")
                Return lst
            End Get
        End Property

        Public ReadOnly Property UserProfileTemplates() As List(Of String)
            Get
                Dim lst As New List(Of String)
                lst.Add("User.Profile")
                lst.Add("Email.Updated.User")
                lst.Add("Email.Updated.Admin")
                Return lst
            End Get
        End Property

        Public ReadOnly Property UserRegistrationTemplates() As List(Of String)
            Get
                Dim lst As New List(Of String)
                lst.Add("User.Register")
                lst.Add("Email.Admin.Created")
                lst.Add("Email.User.Created.Private")
                lst.Add("Email.User.Created.Verified")
                lst.Add("Email.User.Created")
                Return lst
            End Get
        End Property

        Public ReadOnly Property MemberlistTemplates() As List(Of String)
            Get
                Dim lst As New List(Of String)
                lst.Add("User.Profile")
                lst.Add("Member.List")
                Return lst
            End Get
        End Property

        Public ReadOnly Property ModuleOperationModes() As List(Of String)
            Get
                Dim lst As New List(Of String)
                lst.Add("ManageUsers")
                lst.Add("UserProfile")
                lst.Add("UserRegister")
                'lst.Add("Memberlist")
                Return lst
            End Get
        End Property

        Public ReadOnly Property Template_EmailPasswordReset(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Password.Reset." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Password.Reset." & strLocale & ".txt"))
                        End If
                    End If
                End If

                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Password.Reset.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_EmailAccountCreated(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Account.Created." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/Email.Account.Created." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Account.Created.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_Email_RegisterAccount_Admin(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.Admin.Created." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.Admin.Created." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.Admin.Created.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_Email_RegisterAccount_UserPrivate(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created.Private." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created.Private." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created.Private.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_Email_RegisterAccount_UserVerified(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created.Verified." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created.Verified." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created.Verified.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_Email_RegisterAccount_User(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/Email.User.Created.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_Email_ProfileUpdate_Admin(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/Email.Updated.Admin." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/Email.Updated.Admin." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/Email.Updated.Admin.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_Email_ProfileUpdate_User(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/Email.Updated.User." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/Email.Updated.User." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/Email.Updated.User.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_EmailAccountData(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Account.Data." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Account.Data." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.Account.Data.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_EmailAddedToRole(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.AddedToRole." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.AddedToRole." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.AddedToRole.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_EmailRemovedFromRole(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.RemovedFromRole." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.RemovedFromRole." & strLocale & ".txt"))
                        End If
                    End If
                End If
                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/Email.RemovedFromRole.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_CreateAccount(ByVal strLocale As String) As String
            Get

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then
                        If System.IO.File.Exists(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Create." & strLocale & ".txt")) Then
                            Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Create." & strLocale & ".txt"))
                        End If
                    End If
                End If

                Return TemplateController.GetTemplate(Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Create.txt"))

            End Get
        End Property

        Public ReadOnly Property Template_EditAccount(ByVal strLocale As String) As String
            Get

                Dim path As String = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Edit.txt")

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then

                        path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Edit." & strLocale & ".txt")

                        If System.IO.File.Exists(path) Then
                            Return TemplateController.GetTemplate(path)
                        End If

                    End If
                End If

                path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Edit.txt")

                If System.IO.File.Exists(path) Then
                    Return TemplateController.GetTemplate(path)
                End If

                Return DefaultTemplate_EditAccount()


            End Get
        End Property

        Public ReadOnly Property Template_EditProfile(ByVal strLocale As String) As String
            Get

                Dim path As String = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Profile.txt")

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then

                        path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Profile." & strLocale & ".txt")

                        If System.IO.File.Exists(path) Then
                            Return TemplateController.GetTemplate(path)
                        End If

                    End If
                End If

                path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/ManageUsers/" & ModuleId.ToString & "/User.Profile.txt")

                If System.IO.File.Exists(path) Then
                    Return TemplateController.GetTemplate(path)
                End If

                Return DefaultTemplate_EditProfile()

            End Get
        End Property


        Public ReadOnly Property Template_UserRegistration(ByVal strLocale As String) As String
            Get

                Dim path As String = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/User.Register.txt")

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then

                        path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/User.Register." & strLocale & ".txt")

                        If System.IO.File.Exists(path) Then
                            Return TemplateController.GetTemplate(path)
                        End If

                    End If
                End If

                path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserRegister/" & ModuleId.ToString & "/User.Register.txt")

                If System.IO.File.Exists(path) Then
                    Return TemplateController.GetTemplate(path)
                End If

                Return DefaultTemplate_UserRegistration()


            End Get
        End Property

        Public ReadOnly Property Template_UserProfile(ByVal strLocale As String) As String
            Get

                Dim path As String = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/User.Profile.txt")

                If Not strLocale Is Nothing Then
                    If strLocale <> "" Then

                        path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/User.Profile." & strLocale & ".txt")

                        If System.IO.File.Exists(path) Then
                            Return TemplateController.GetTemplate(path)
                        End If

                    End If
                End If

                path = Server.MapPath(Me.TemplateSourceDirectory & "/Templates/UserProfile/" & ModuleId.ToString & "/User.Profile.txt")

                If System.IO.File.Exists(path) Then
                    Return TemplateController.GetTemplate(path)
                End If

                Return DefaultTemplate_UserProfile()


            End Get
        End Property

        

#End Region

#Region "Common Properties"

        Public ReadOnly Property DecorateForm() As String
            Get
                If Not Settings("DecorateForm") Is Nothing Then
                    Return Convert.ToBoolean(Settings("DecorateForm"))
                End If
                Return True
            End Get
        End Property

        Public ReadOnly Property ControlSkin() As String
            Get
                If Not Settings("ControlSkin") Is Nothing Then
                    Return Convert.ToString(Settings("ControlSkin"))
                End If
                Return "Simple"
            End Get
        End Property

        Public ReadOnly Property CurrentLocale() As String
            Get
                Return CType(Page, PageBase).PageCulture.Name
            End Get
        End Property

        Public ReadOnly Property ModuleDirectory() As String
            Get
                Return "~/Desktopmodules/Admin/dnnWerk.Users"
            End Get
        End Property

        Protected ReadOnly Property ProfileResourcefile() As String
            Get
                Return ResolveUrl("~/Desktopmodules/Admin/Security/App_LocalResources/Profile.ascx")
            End Get
        End Property

        Protected ReadOnly Property SharedResourcefile() As String
            Get
                Return ResolveUrl("~/Desktopmodules/Admin/dnnWerk.Users/App_LocalResources/UserModule.ascx")
            End Get
        End Property

        Protected ReadOnly Property User() As Entities.Users.UserInfo
            Get
                Dim oUser As UserInfo = Nothing
                If Not Request.QueryString("uid") Is Nothing Then
                    If Microsoft.VisualBasic.IsNumeric(Request.QueryString("uid")) Then
                        oUser = UserController.GetUserById(PortalId, Convert.ToInt32(Request.QueryString("uid")))
                    End If
                Else
                    oUser = UserController.GetCurrentUserInfo
                End If
                Return oUser
            End Get
        End Property

        Public ReadOnly Property IsUser() As Boolean
            Get
                If Not User Is Nothing Then
                    If User.UserID = UserInfo.UserID Then
                        Return True
                    End If
                End If
                Return False
            End Get
        End Property

        Public ReadOnly Property IsAdmin() As Boolean
            Get
                Return UserInfo.IsInRole(PortalSettings.AdministratorRoleName)
            End Get
        End Property

#End Region

#Region "Shared Helper Methods"

        Protected Function IsValidUserAttribute(ByVal Attribute As String, ByVal objRoot As Control) As Boolean

            Dim id As String = ""
            Select Case Attribute
                Case Constants.User_Username
                    id = Constants.ControlId_Username
                Case Constants.User_Displayname
                    id = Constants.ControlId_Displayname
                Case Constants.User_Firstname
                    id = Constants.ControlId_Firstname
                Case Constants.User_Lastname
                    id = Constants.ControlId_Lastname
                Case Constants.User_Password1
                    id = Constants.ControlId_Password1
                Case Constants.User_Password2
                    id = Constants.ControlId_Password2
                Case Constants.User_Email
                    id = Constants.ControlId_Email
            End Select

            Dim txtbox As RadTextBox = CType(FindControlRecursive(objRoot, objRoot.ID & "_" & id), RadTextBox)
            If Not txtbox Is Nothing Then
                If txtbox.Text <> "" Then
                    Return True
                End If
            End If
            Return False

        End Function

        Protected Function IsValidProperty(ByVal objUser As UserInfo, ByVal ProfProperty As ProfilePropertyDefinition, ByRef objControl As Control) As Boolean

            If ProfProperty.Required = False Then
                Return True
            End If

            Dim ctl As Control = GetPropertyControl(ProfProperty, objControl)
            If Not ctl Is Nothing Then

                If TypeOf (ctl) Is Telerik.Web.UI.RadComboBox Then
                    If CType(ctl, Telerik.Web.UI.RadComboBox).Text <> "" Then
                        Return True
                    Else
                        Return False
                    End If
                ElseIf TypeOf (ctl) Is Telerik.Web.UI.RadTextBox Then
                    If CType(ctl, Telerik.Web.UI.RadTextBox).Text <> "" Then
                        Return True
                    Else
                        Return False
                    End If
                ElseIf TypeOf (ctl) Is Telerik.Web.UI.RadDateTimePicker Then
                    If Not CType(ctl, Telerik.Web.UI.RadDateTimePicker).DbSelectedDate Is Nothing Then
                        Return True
                    Else
                        Return False
                    End If
                ElseIf TypeOf (ctl) Is Telerik.Web.UI.RadDatePicker Then
                    If Not CType(ctl, Telerik.Web.UI.RadDatePicker).DbSelectedDate Is Nothing Then
                        Return True
                    Else
                        Return False
                    End If
                ElseIf TypeOf (ctl) Is Telerik.Web.UI.RadNumericTextBox Then
                    If CType(ctl, RadNumericTextBox).Value >= 0 Then
                        Return True
                    Else
                        Return False
                    End If
                ElseIf TypeOf (ctl) Is Telerik.Web.UI.RadAsyncUpload Then
                    If Not objUser Is Nothing Then
                        If String.IsNullOrEmpty(UserInfo.Profile.GetPropertyValue(ProfProperty.PropertyName)) Then
                            If CType(ctl, RadAsyncUpload).UploadedFiles.Count > 0 Then
                                Return True
                            Else
                                Return False
                            End If
                        Else
                            Return True
                        End If
                    Else
                        If CType(ctl, RadAsyncUpload).UploadedFiles.Count > 0 Then
                            Return True
                        Else
                            Return False
                        End If
                    End If
                    
                ElseIf TypeOf (ctl) Is CheckBox Then
                    If Not CType(ctl, CheckBox).Checked = False Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            End If

            Return True

        End Function

        Protected Function GetPropertyType(ByVal TypeId As Integer) As String
            Dim lc As New DotNetNuke.Common.Lists.ListController
            Dim entry As Lists.ListEntryInfo = lc.GetListEntryInfo(TypeId)
            If Not entry Is Nothing Then
                Return entry.Value
            End If
            Return ""
        End Function

        Public Function FindControlRecursive(ByVal objRoot As Control, ByVal id As String) As Control
            If objRoot.ID = id Then
                Return objRoot
            End If
            For Each c As Control In objRoot.Controls
                Dim t As Control = FindControlRecursive(c, id)
                If Not t Is Nothing Then
                    Return t
                End If
            Next
            Return Nothing
        End Function

        Protected Function IsRegisterMode() As Boolean
            Return (Request.IsAuthenticated = False)
        End Function

 

        Protected Function DefaultTemplate_UserRegistration() As String

            Dim strItem As String = ""
            Dim sb As New StringBuilder

            strItem = Constants.DefaultItemTemplate
            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Username")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Username")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Username")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Username")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Displayname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Displayname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Displayname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Displayname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Firstname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Firstname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Firstname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Firstname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Lastname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Lastname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Lastname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Lastname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Email")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Email")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Email")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Email")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Password1")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password1")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password1")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password1")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Password2")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password2")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password2")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password2")
            sb.Append(strItem & vbCrLf & vbCrLf)

            Dim props As ProfilePropertyDefinitionCollection = ProfileController.GetPropertyDefinitionsByPortal(PortalSettings.PortalId)
            For Each prop As ProfilePropertyDefinition In props

                strItem = Constants.DefaultItemTemplate

                strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:PROFILEHELP:" & prop.PropertyName)
                strItem = strItem.Replace("RESX:PROPERTY", "RESX:PROFILE:" & prop.PropertyName)
                strItem = strItem.Replace("CTL:PROPERTY", "CTL:PROFILE:" & prop.PropertyName)
                strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:PROFILE:" & prop.PropertyName)
                sb.Append(strItem & vbCrLf & vbCrLf)

            Next

            sb.Append("<div class=""reg_Command"">" & vbCrLf)
            sb.Append("  <div class=""Left"">[CTL:UPDATEBUTTON:Create Account]</div>" & vbCrLf)
            sb.Append("  <div class=""ucmClear""></div>" & vbCrLf)
            sb.Append("</div>" & vbCrLf)


            Return sb.ToString

        End Function

        Protected Function DefaultTemplate_UserProfile() As String

            Dim strItem As String = ""
            Dim sb As New StringBuilder

            strItem = Constants.DefaultItemTemplate
            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Username")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Username")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Username")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Username")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Displayname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Displayname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Displayname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Displayname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Firstname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Firstname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Firstname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Firstname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Lastname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Lastname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Lastname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Lastname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Email")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Email")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Email")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Email")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Password1")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password1")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password1")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password1")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Password2")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password2")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password2")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password2")
            sb.Append(strItem & vbCrLf & vbCrLf)

            Dim props As ProfilePropertyDefinitionCollection = ProfileController.GetPropertyDefinitionsByPortal(PortalSettings.PortalId)
            For Each prop As ProfilePropertyDefinition In props

                strItem = Constants.DefaultItemTemplate

                strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:PROFILEHELP:" & prop.PropertyName)
                strItem = strItem.Replace("RESX:PROPERTY", "RESX:PROFILE:" & prop.PropertyName)
                strItem = strItem.Replace("CTL:PROPERTY", "CTL:PROFILE:" & prop.PropertyName)
                strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:PROFILE:" & prop.PropertyName)
                sb.Append(strItem & vbCrLf & vbCrLf)

            Next

            sb.Append("<div class=""reg_Command"">" & vbCrLf)
            sb.Append("  <div class=""Left"">[CTL:UPDATEBUTTON:Update Account]</div>" & vbCrLf)
            sb.Append("  <div class=""Right"">[CTL:DELETEBUTTON:Close Account]</div>" & vbCrLf)
            sb.Append("  <div class=""ucmClear""></div>" & vbCrLf)
            sb.Append("</div>" & vbCrLf)


            Return sb.ToString

        End Function

        Protected Function DefaultTemplate_EditProfile() As String

            Dim strItem As String = ""
            Dim sb As New StringBuilder

            strItem = Constants.DefaultItemTemplate
            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Username")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Username")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Username")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Username")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Displayname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Displayname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Displayname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Displayname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Firstname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Firstname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Firstname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Firstname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Lastname")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Lastname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Lastname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Lastname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Email")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Email")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Email")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Email")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Password1")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password1")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password1")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password1")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:USERHELP:Password2")
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password2")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password2")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password2")
            sb.Append(strItem & vbCrLf & vbCrLf)

            Dim props As ProfilePropertyDefinitionCollection = ProfileController.GetPropertyDefinitionsByPortal(PortalSettings.PortalId)
            For Each prop As ProfilePropertyDefinition In props

                strItem = Constants.DefaultItemTemplate

                strItem = strItem.Replace("RESX:PROPERTYHELP", "RESX:PROFILEHELP:" & prop.PropertyName)
                strItem = strItem.Replace("RESX:PROPERTY", "RESX:PROFILE:" & prop.PropertyName)
                strItem = strItem.Replace("CTL:PROPERTY", "CTL:PROFILE:" & prop.PropertyName)
                strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:PROFILE:" & prop.PropertyName)
                sb.Append(strItem & vbCrLf & vbCrLf)

            Next

            sb.Append("<div class=""reg_Command"">" & vbCrLf)
            sb.Append("  <div class=""Left"">[CTL:UPDATEBUTTON:Update Account]</div>" & vbCrLf)
            sb.Append("  <div class=""Right"">[CTL:DELETEBUTTON:Close Account]</div>" & vbCrLf)
            sb.Append("  <div class=""ucmClear""></div>" & vbCrLf)
            sb.Append("</div>" & vbCrLf)


            Return sb.ToString

        End Function

        Protected Function DefaultTemplate_EditAccount() As String

            Dim strItem As String = ""
            Dim sb As New StringBuilder

            strItem = Constants.DefaultItemTemplate
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Username")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Username")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Username")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Displayname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Displayname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Displayname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Email")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Email")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Email")
            sb.Append(strItem & vbCrLf & vbCrLf)

            Return sb.ToString

        End Function

        Protected Function DefaultTemplate_CreateAccount() As String

            Dim strItem As String = ""
            Dim sb As New StringBuilder

            strItem = Constants.DefaultItemTemplate
            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Username")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Username")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Username")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Displayname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Displayname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Displayname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Firstname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Firstname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Firstname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Lastname")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Lastname")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Lastname")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Email")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Email")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Email")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password1")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password1")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password1")
            sb.Append(strItem & vbCrLf & vbCrLf)

            strItem = Constants.DefaultItemTemplate

            strItem = strItem.Replace("RESX:PROPERTY", "RESX:USER:Password2")
            strItem = strItem.Replace("CTL:PROPERTY", "CTL:USER:Password2")
            strItem = strItem.Replace("REQUIRED:PROPERTY", "REQUIRED:USER:Password2")
            sb.Append(strItem & vbCrLf & vbCrLf)

            Return sb.ToString

        End Function

#End Region

#Region "Shared DataAccess Helpers"

        Protected Function GetPropertyValueList(ByVal PropertyDefinition As ProfilePropertyDefinition) As List(Of String)

            Dim lst As List(Of String) = Nothing

            If Not DataCache.GetCache("dnnwerk_ValueList_" & PropertyDefinition.PropertyDefinitionId.ToString) Is Nothing Then
                lst = CType(DataCache.GetCache("dnnwerk_ValueList_" & PropertyDefinition.PropertyDefinitionId.ToString), List(Of String))
            End If

            If lst Is Nothing Then

                lst = New List(Of String)

                Dim dr As IDataReader = Nothing
                If FilterSelectByRole = Null.NullInteger Then
                    dr = DotNetNuke.Data.DataProvider.Instance().ExecuteReader("dnnWerk_Users_GetValueList", PropertyDefinition.PropertyDefinitionId, DBNull.Value)
                Else
                    dr = DotNetNuke.Data.DataProvider.Instance().ExecuteReader("dnnWerk_Users_GetValueList", PropertyDefinition.PropertyDefinitionId, FilterSelectByRole)
                End If


                If Not dr Is Nothing Then
                    While dr.Read

                        Dim strValueSimpliefied As String = Convert.ToString(dr("PropertyValue"))

                        If strValueSimpliefied.Contains(",") Then

                            Dim strValue As String() = strValueSimpliefied.Split(Char.Parse(","))
                            For Each strItem As String In strValue

                                If strItem.Trim.Length > 0 Then
                                    If Not lst.Contains(strItem) Then
                                        lst.Add(strItem)
                                    End If
                                End If
                            Next

                        Else

                            If Not lst.Contains(strValueSimpliefied) Then
                                If strValueSimpliefied.Trim.Length > 0 Then
                                    lst.Add(strValueSimpliefied)
                                End If
                            End If

                        End If


                    End While

                    dr.Close()
                    dr.Dispose()

                    'DataCache.SetCache("dnnwerk_ValueList_" & PropertyDefinition.PropertyDefinitionId.ToString, lst)

                End If


            End If

            Return lst

        End Function

        Protected Function GetUsers() As DataSet

            Dim ds As DataSet = Nothing

            If Not DataCache.GetCache("dnnwerk_Userlist") Is Nothing Then
                ds = CType(DataCache.GetCache("dnnwerk_Userlist"), DataSet)
            End If

            If ds Is Nothing Then

                If FilterByRole = Null.NullString Then
                    ds = DotNetNuke.Data.DataProvider.Instance().ExecuteDataSet("dnnWerk_Users_ListByPortal", PortalId)
                Else
                    ds = DotNetNuke.Data.DataProvider.Instance().ExecuteDataSet("dnnWerk_Users_ListByRole", PortalId, FilterByRole)
                End If

                DataCache.SetCache("dnnwerk_Userlist", ds)

            End If

            Return ds

        End Function

#End Region

#Region "Shared Template Processing Methods"

        Protected Sub ProcessDisplayTemplate(ByRef plhControls As PlaceHolder, ByVal objItem As DataRowView, ByVal Template As String)

            Dim literal As New Literal
            Dim delimStr As String = "[]"
            Dim delimiter As Char() = delimStr.ToCharArray()

            Dim templateArray As String()
            templateArray = Template.Split(delimiter)

            For iPtr As Integer = 0 To templateArray.Length - 1 Step 2

                Dim strHTML As String = templateArray(iPtr).ToString()
                plhControls.Controls.Add(New LiteralControl(strHTML))

                If iPtr < templateArray.Length - 1 Then

                    Dim strToken As String = templateArray(iPtr + 1)

                    Select Case strToken

                        Case "USERID"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = objItem("UserId")
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try

                        Case "USERNAME"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = objItem("Username")
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try


                        Case "CREATEDDATE"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = Convert.ToDateTime(objItem("CreatedDate")).ToLongDateString
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try


                        Case "CREATEDDATELONG"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = Convert.ToDateTime(objItem("CreatedDate")).ToLongDateString
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try

                        Case "FIRSTNAME"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = objItem("FirstName")
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try


                        Case "LASTNAME"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = objItem("LastName")
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try


                        Case "EMAIL"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = objItem("Email")
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try


                        Case "DISPLAYNAME"

                            Try
                                Dim objLiteral As New Literal
                                objLiteral.Text = objItem("DisplayName")
                                objLiteral.EnableViewState = False
                                plhControls.Controls.Add(objLiteral)
                            Catch ex As Exception

                            End Try


                        Case Else

                            If (strToken.StartsWith("PROFILE:")) Then

                                Dim strProperty As String = strToken.Substring(8, strToken.Length - 8)

                                Dim prop As ProfilePropertyDefinition = ProfileController.GetPropertyDefinitionByName(PortalSettings.PortalId, strProperty)
                                If Not prop Is Nothing Then

                                    AddPropertyValue(plhControls, prop, objItem)

                                End If

                            End If

                    End Select

                End If

            Next

        End Sub

        Protected Sub ProcessFormTemplate(ByRef plhControls As PlaceHolder, ByVal Template As String, ByVal objUser As UserInfo, ByRef PageNeedsToPostback As Boolean)


            plhControls.Controls.Clear()
            Dim strIdent As String = plhControls.ID & "_"

            Dim literal As New Literal
            Dim delimStr As String = "[]"
            Dim delimiter As Char() = delimStr.ToCharArray()

            Dim templateArray As String()
            templateArray = Template.Split(delimiter)

            For iPtr As Integer = 0 To templateArray.Length - 1 Step 2

                Dim strHTML As String = templateArray(iPtr).ToString()
                plhControls.Controls.Add(New LiteralControl(strHTML))

                If iPtr < templateArray.Length - 1 Then

                    Dim strToken As String = templateArray(iPtr + 1)

                    If strToken.StartsWith("RESX:") Then

                        Dim strKey As String = strToken.Split(Char.Parse(":"))(2)
                        Dim strTarget As String = strToken.Split(Char.Parse(":"))(1)
                        Dim strValue As String = Localization.GetString(strKey, SharedResourcefile)

                        If strTarget.ToUpper = "USER" Then
                            strValue = Localization.GetString(strKey, SharedResourcefile)
                        End If

                        If strTarget.ToUpper = "USERHELP" Then
                            strValue = Localization.GetString(strKey & ".Help", SharedResourcefile)
                        End If

                        If strTarget.ToUpper = "PROFILE" Then
                            strValue = Localization.GetString("ProfileProperties_" & strKey, ProfileResourcefile)
                        End If

                        If strTarget.ToUpper = "PROFILEHELP" Then
                            strValue = Localization.GetString("ProfileProperties_" & strKey & ".Help", ProfileResourcefile)
                        End If

                        If strValue = "" Then
                            strValue = strKey
                        End If

                        If strValue <> "" Then
                            Dim objLiteral As New Literal
                            objLiteral.Text = strValue
                            objLiteral.ID = plhControls.ID & "_RESX_" & strTarget & "_" & strKey
                            objLiteral.EnableViewState = False
                            plhControls.Controls.Add(objLiteral)
                        End If



                    ElseIf strToken.StartsWith("LBL:") Then

                        If Not objUser Is Nothing Then

                            Dim strControlToken As String = strToken.Substring(4, strToken.Length - 4)
                            Dim strType As String = strControlToken.Substring(0, strControlToken.IndexOf(":"))
                            Dim strProperty As String = strControlToken.Substring(strControlToken.IndexOf(":") + 1)
                            Dim strvalue As String = ""

                            If strType.ToUpper = "PROFILE" Then

                                Dim prop As ProfilePropertyDefinition = ProfileController.GetPropertyDefinitionByName(PortalSettings.PortalId, strProperty)
                                If Not prop Is Nothing Then
                                    Try
                                        strvalue = objUser.Profile.GetPropertyValue(prop.PropertyName)
                                    Catch
                                    End Try
                                End If

                            ElseIf strType.ToUpper = "USER" Then

                                Select Case strProperty.ToUpper
                                    Case "USERNAME"
                                        strvalue = objUser.Username
                                    Case "FIRSTNAME"
                                        strvalue = objUser.FirstName
                                    Case "LASTNAME"
                                        strvalue = objUser.LastName
                                    Case "EMAIL"
                                        strvalue = objUser.Email
                                    Case "DISPLAYNAME"
                                        strvalue = objUser.DisplayName
                                    Case "MEMBERSINCE"
                                        strvalue = objUser.CreatedOnDate.ToShortDateString
                                End Select

                            End If

                            Dim ctl As New Label
                            ctl.CssClass = "UserobjectLabel"
                            ctl.EnableViewState = False
                            ctl.Text = strvalue
                            plhControls.Controls.Add(ctl)


                        End If

                    ElseIf strToken.StartsWith("CTL:") Then

                        Dim strControlToken As String = ""
                        Try
                            strControlToken = strToken.Substring(4, strToken.Length - 4)
                        Catch
                        End Try

                        Dim strType As String = ""
                        Try
                            strType = strControlToken.Substring(0, strControlToken.IndexOf(":"))
                        Catch
                        End Try

                        Dim strProperty As String = ""
                        Try
                            strProperty = strControlToken.Substring(strControlToken.IndexOf(":") + 1)
                        Catch
                        End Try


                        If strType.ToUpper = "PROFILE" Then

                            Dim prop As ProfilePropertyDefinition = ProfileController.GetPropertyDefinitionByName(PortalSettings.PortalId, strProperty)
                            If Not prop Is Nothing Then

                                AddPropertyControl(prop, plhControls, prop.Visible, objUser, PageNeedsToPostback)

                            End If

                        ElseIf strType.ToUpper = "UPDATEBUTTON" Then

                            Dim btn As New Button
                            btn.Text = strProperty
                            btn.ID = plhControls.ID & "_" & Constants.ControlId_UpdateButton
                            plhControls.Controls.Add(btn)

                        ElseIf strType.ToUpper = "DELETEBUTTON" Then

                            Dim btn As New Button
                            btn.Text = strProperty
                            btn.ID = plhControls.ID & "_" & Constants.ControlId_DeleteButton
                            plhControls.Controls.Add(btn)

                        ElseIf strType.ToUpper = "USER" Then

                            Select Case strProperty.ToUpper

                                Case "USERNAME"

                                    If Not objUser Is Nothing Then
                                        Dim ctl As New Label
                                        ctl.CssClass = "UsernameLabel"
                                        ctl.ID = plhControls.ID & "_" & Constants.LabelId_Username
                                        ctl.EnableViewState = False

                                        Try
                                            ctl.Text = Convert.ToString(objUser.Username)
                                        Catch
                                        End Try
                                        plhControls.Controls.Add(ctl)
                                    Else
                                        Dim ctl As New RadTextBox
                                        ctl.ID = plhControls.ID & "_" & Constants.ControlId_Username
                                        ctl.Skin = ControlSkin
                                        ctl.EnableAjaxSkinRendering = True
                                        ctl.ClientEvents.OnBlur = "ValidateUsername"
                                        ctl.ClientEvents.OnFocus = "SelectField"
                                        Try
                                            ctl.Text = Convert.ToString(objUser.Username)
                                        Catch
                                        End Try
                                        ctl.Enabled = True
                                        plhControls.Controls.Add(ctl)
                                    End If


                                Case "FIRSTNAME"

                                    Dim ctl As New RadTextBox
                                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_Firstname
                                    ctl.Skin = ControlSkin
                                    ctl.EnableAjaxSkinRendering = True
                                    ctl.ClientEvents.OnBlur = "UnSelectField"
                                    ctl.ClientEvents.OnFocus = "SelectField"
                                    Try
                                        ctl.Text = Convert.ToString(objUser.FirstName)
                                    Catch
                                    End Try
                                    ctl.Enabled = True
                                    plhControls.Controls.Add(ctl)

                                Case "PASSWORD1"

                                    Dim ctl As New RadTextBox
                                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_Password1
                                    ctl.Skin = ControlSkin
                                    ctl.TextMode = InputMode.Password
                                    ctl.Enabled = True
                                    ctl.EnableAjaxSkinRendering = True
                                    ctl.ClientEvents.OnFocus = "SelectField"
                                    ctl.ClientEvents.OnBlur = "ValidatePasswordPolicy"
                                   
                                    plhControls.Controls.Add(ctl)

                                Case "PASSWORD2"

                                    Dim ctl As New RadTextBox
                                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_Password2
                                    ctl.Skin = ControlSkin
                                    ctl.TextMode = InputMode.Password
                                    ctl.Enabled = True                                
                                    ctl.ClientEvents.OnFocus = "SelectField"
                                    ctl.ClientEvents.OnBlur = "ValidatePasswords"
                                    ctl.EnableAjaxSkinRendering = True
                                    plhControls.Controls.Add(ctl)

                                Case "LASTNAME"

                                    Dim ctl As New RadTextBox
                                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_Lastname
                                    ctl.Skin = ControlSkin
                                    Try
                                        ctl.Text = Convert.ToString(objUser.LastName)
                                    Catch
                                    End Try
                                    ctl.Enabled = True
                                    ctl.EnableAjaxSkinRendering = True
                                    ctl.ClientEvents.OnBlur = "UnSelectField"
                                    ctl.ClientEvents.OnFocus = "SelectField"
                                    plhControls.Controls.Add(ctl)

                                Case "EMAIL"

                                    Dim ctl As New RadTextBox
                                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_Email
                                    ctl.Skin = ControlSkin
                                    Try
                                        ctl.Text = Convert.ToString(objUser.Email)
                                    Catch
                                    End Try
                                    ctl.ClientEvents.OnBlur = "UnSelectField"
                                    ctl.ClientEvents.OnFocus = "SelectField"
                                    ctl.EnableAjaxSkinRendering = True
                                    ctl.Enabled = True

                                    plhControls.Controls.Add(ctl)

                                Case "DISPLAYNAME"

                                    Dim ctl As New RadTextBox
                                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_Displayname
                                    ctl.Skin = ControlSkin
                                    Try
                                        ctl.Text = Convert.ToString(objUser.DisplayName)
                                    Catch
                                    End Try
                                    ctl.Enabled = True
                                    ctl.EnableAjaxSkinRendering = True
                                    ctl.ClientEvents.OnBlur = "UnSelectField"
                                    ctl.ClientEvents.OnFocus = "SelectField"
                                    plhControls.Controls.Add(ctl)

                            End Select

                        End If



                    ElseIf strToken.StartsWith("ERROR:") Then

                        Dim strProperty As String = strToken.Substring(6, strToken.Length - 6)
                        Dim prop As ProfilePropertyDefinition = ProfileController.GetPropertyDefinitionByName(PortalSettings.PortalId, strProperty)
                        If Not prop Is Nothing Then
                            Dim pnlError As New Panel
                            pnlError.ID = plhControls.ID & "_" & Constants.ControlId_PanelError & prop.PropertyDefinitionId.ToString
                            pnlError.CssClass = Constants.CssClass_ErrorPanel
                            pnlError.Visible = False
                            pnlError.Controls.Add(New LiteralControl("<img src=""" & Me.TemplateSourceDirectory & "/images/error.png"">"))
                            plhControls.Controls.Add(pnlError)
                        Else
                            Dim pnlError As New Panel
                            pnlError.ID = plhControls.ID & "_" & Constants.ControlId_PanelError & strProperty
                            pnlError.CssClass = Constants.CssClass_ErrorPanel
                            pnlError.Visible = False
                            pnlError.Controls.Add(New LiteralControl("<img src=""" & Me.TemplateSourceDirectory & "/images/error.png"">"))
                            plhControls.Controls.Add(pnlError)
                        End If

                    ElseIf strToken.StartsWith("VALIDATE:") Then

                        Dim strTarget As String = strToken.Split(Char.Parse(":"))(1)
                        Dim strProperty As String = strToken.Split(Char.Parse(":"))(2)

                        If strTarget.ToUpper = "USER" Then

                        End If

                    ElseIf strToken.StartsWith("REQUIRED:") Then

                        Dim strTarget As String = strToken.Split(Char.Parse(":"))(1)
                        Dim strProperty As String = strToken.Split(Char.Parse(":"))(2)

                        If strTarget.ToUpper = "PROFILE" Then

                            Dim prop As ProfilePropertyDefinition = ProfileController.GetPropertyDefinitionByName(PortalSettings.PortalId, strProperty)
                            If Not prop Is Nothing Then
                                If prop.Required Then

                                    Dim pnlRequired As New Panel
                                    pnlRequired.ID = plhControls.ID & "_" & Constants.ControlId_PanelRequired & prop.PropertyDefinitionId.ToString
                                    pnlRequired.CssClass = Constants.CssClass_RequiredPanel
                                    pnlRequired.Visible = True
                                    pnlRequired.Controls.Add(New LiteralControl("<img src=""" & Me.TemplateSourceDirectory & "/images/required.png"" class=""imgIndicator"">"))
                                    plhControls.Controls.Add(pnlRequired)

                                    Dim pnlError As New Panel
                                    pnlError.ID = plhControls.ID & "_" & Constants.ControlId_PanelError & prop.PropertyDefinitionId.ToString
                                    pnlError.CssClass = Constants.CssClass_ErrorPanel
                                    pnlError.Visible = False
                                    pnlError.Controls.Add(New LiteralControl("<img src=""" & Me.TemplateSourceDirectory & "/images/error.png"" class=""imgIndicator"">"))
                                    plhControls.Controls.Add(pnlError)

                                End If
                            End If

                        ElseIf strTarget.ToUpper = "USER" Then

                            Dim blnNeededIndicator As Boolean = True

                            If strProperty.ToLower = "username" AndAlso (objUser Is Nothing = False) Then
                                blnNeededIndicator = False
                            End If

                            If blnNeededIndicator Then

                                Dim pnlRequired As New Panel
                                pnlRequired.ID = plhControls.ID & "_" & Constants.ControlId_PanelRequired & strProperty
                                pnlRequired.CssClass = Constants.CssClass_RequiredPanel
                                pnlRequired.Visible = True
                                pnlRequired.Controls.Add(New LiteralControl("<img src=""" & Me.TemplateSourceDirectory & "/images/required.png"" class=""imgIndicator"">"))
                                plhControls.Controls.Add(pnlRequired)

                                Dim pnlError As New Panel
                                pnlError.ID = plhControls.ID & "_" & Constants.ControlId_PanelError & strProperty
                                pnlError.CssClass = Constants.CssClass_ErrorPanel
                                pnlError.Visible = False
                                pnlError.Controls.Add(New LiteralControl("<img src=""" & Me.TemplateSourceDirectory & "/images/error.png"" class=""imgIndicator"">"))
                                plhControls.Controls.Add(pnlError)

                                If strProperty.ToLower = "username" OrElse strProperty.ToLower = "password1" OrElse strProperty.ToLower = "password2" OrElse strProperty.ToLower = "email" Then

                                    Dim pnlSuccess As New Panel
                                    pnlSuccess.ID = plhControls.ID & "_" & Constants.ControlId_PanelSuccess & strProperty
                                    pnlSuccess.CssClass = Constants.CssClass_SuccessPanel
                                    pnlSuccess.Visible = False
                                    pnlSuccess.Controls.Add(New LiteralControl("<img src=""" & Me.TemplateSourceDirectory & "/images/accept.png"" class=""imgIndicator"">"))
                                    plhControls.Controls.Add(pnlSuccess)

                                End If

                            End If

                        End If


                    End If


                End If

            Next

        End Sub

        Protected Sub AddPropertyValue(ByRef plhControls As PlaceHolder, ByVal ProfileProperty As ProfilePropertyDefinition, ByVal objItem As DataRowView)

            Dim strType As String = GetPropertyType(ProfileProperty.DataType)

            Select Case strType.ToLower

                Case "list"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = objItem(ProfileProperty.PropertyName)
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "country"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = objItem(ProfileProperty.PropertyName)
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "date"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToDateTime(objItem(ProfileProperty.PropertyName)).ToShortDateString
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)


                Case "datetime"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToDateTime(objItem(ProfileProperty.PropertyName)).ToLongDateString
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "integer"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToString(objItem(ProfileProperty.PropertyName))
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "region"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToString(objItem(ProfileProperty.PropertyName))
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "locale"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToString(objItem(ProfileProperty.PropertyName))
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "file"

                    If Convert.ToString(objItem(ProfileProperty.PropertyName)) <> "" Then
                        'add view control
                        Dim objLiteral As New Literal
                        objLiteral.Text = "<a href=""" & PortalSettings.HomeDirectory & Convert.ToString(objItem(ProfileProperty.PropertyName)) & ">Herunterladen</a>"
                        objLiteral.EnableViewState = False
                        plhControls.Controls.Add(objLiteral)
                    End If


                Case "image"

                    If Convert.ToString(objItem(ProfileProperty.PropertyName)) <> "" Then
                        'add view control
                        Dim objLiteral As New Literal
                        objLiteral.Text = "<img src=""" & PortalSettings.HomeDirectory & Convert.ToString(objItem(ProfileProperty.PropertyName)) & """ alt=""" & LocalizeProperty(ProfileProperty) & """ />"
                        objLiteral.EnableViewState = False
                        plhControls.Controls.Add(objLiteral)
                    End If


                Case "richtext"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToString(objItem(ProfileProperty.PropertyName))
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "timezone"

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToString(objItem(ProfileProperty.PropertyName))
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case "truefalse"

                    'add view control
                    Dim objLiteral As New Literal
                    If Convert.ToBoolean(objItem(ProfileProperty.PropertyName)) Then
                        objLiteral.Text = "Ja"
                    Else
                        objLiteral.Text = "Nein"
                    End If

                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)

                Case Else

                    'add view control
                    Dim objLiteral As New Literal
                    objLiteral.Text = Convert.ToString(objItem(ProfileProperty.PropertyName))
                    objLiteral.EnableViewState = False
                    plhControls.Controls.Add(objLiteral)


            End Select


        End Sub

        Protected Sub AddPropertyControl(ByVal ProfileAttribute As ProfilePropertyDefinition, ByRef plhControls As PlaceHolder, ByVal IsEnabled As Boolean, ByVal objUser As UserInfo, ByRef PageNeedsToPostback As Boolean)

            Dim strType As String = GetPropertyType(ProfileAttribute.DataType)

            Select Case strType.ToLower
                Case "list"
                    Dim drp As New Telerik.Web.UI.RadComboBox
                    drp.Skin = ControlSkin
                    drp.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    drp.Attributes.Add("ParentId", plhControls.ID)
                    Dim entries As New Lists.ListEntryInfoCollection
                    Dim lc As New Lists.ListController
                    entries = lc.GetListEntryInfoCollection(ProfileAttribute.PropertyName)
                    For Each entry As Lists.ListEntryInfo In entries
                        drp.Items.Add(New Telerik.Web.UI.RadComboBoxItem(entry.Text, entry.Value))
                    Next
                    Try
                        drp.SelectedValue = objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName)
                    Catch
                    End Try
                    drp.Enabled = IsEnabled
                    drp.EnableAjaxSkinRendering = True
                    drp.OnClientBlur = "UnSelectField"
                    drp.OnClientFocus = "SelectField"
                    plhControls.Controls.Add(drp)
                Case "country"
                    Dim ctl As New Telerik.Web.UI.RadComboBox
                    ctl.Skin = Me.ControlSkin
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Attributes.Add("ParentId", plhControls.ID)
                    Dim entries As New Lists.ListEntryInfoCollection
                    Dim lc As New Lists.ListController
                    entries = lc.GetListEntryInfoCollection("Country")
                    For Each entry As Lists.ListEntryInfo In entries
                        ctl.Items.Add(New Telerik.Web.UI.RadComboBoxItem(entry.Text, entry.Value))
                    Next
                    Try
                        ctl.SelectedValue = objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName)
                    Catch
                        ctl.Items(0).Selected = True
                    End Try
                    ctl.Enabled = IsEnabled
                    ctl.EnableAjaxSkinRendering = True
                    ctl.AutoPostBack = True
                    ctl.OnClientBlur = "UnSelectField"
                    ctl.OnClientFocus = "SelectField"

                    AddHandler ctl.SelectedIndexChanged, AddressOf drpCountry_SelectedIndexChanged

                    plhControls.Controls.Add(ctl)

                Case "date"
                    Dim ctl As New RadDatePicker
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Skin = ControlSkin
                    ctl.MinDate = New Date(1900, 1, 1)
                    ctl.MaxDate = New Date(2100, 12, 31)

                    Try
                        ctl.SelectedDate = Convert.ToDateTime(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try                   
                    ctl.Enabled = IsEnabled
                    ctl.EnableAjaxSkinRendering = True
                    ctl.DateInput.ClientEvents.OnBlur = "UnSelectField"
                    ctl.DateInput.ClientEvents.OnFocus = "SelectField"
                    plhControls.Controls.Add(ctl)
                Case "datetime"
                    Dim ctl As New RadDateTimePicker
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Skin = ControlSkin
                    ctl.MinDate = New Date(1900, 1, 1)
                    ctl.MaxDate = New Date(2100, 12, 31)
                    Try
                        ctl.SelectedDate = Convert.ToDateTime(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try
                    ctl.Enabled = IsEnabled                   
                    ctl.EnableAjaxSkinRendering = True
                    ctl.DateInput.ClientEvents.OnBlur = "UnSelectField"
                    ctl.DateInput.ClientEvents.OnFocus = "SelectField"

                    plhControls.Controls.Add(ctl)
                Case "integer"
                    Dim ctl As New RadNumericTextBox
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Skin = ControlSkin
                    Try
                        ctl.Value = Convert.ToDouble(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try
                    ctl.Enabled = IsEnabled
                    ctl.EnableAjaxSkinRendering = True
                    ctl.ClientEvents.OnBlur = "UnSelectField"
                    ctl.ClientEvents.OnFocus = "SelectField"
                    ctl.MaxLength = ProfileAttribute.Length
                    plhControls.Controls.Add(ctl)

                Case "region"

                    Dim ctlDrop As New RadComboBox
                    ctlDrop.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & Constants.ControlId_DropdownSuffix & ProfileAttribute.PropertyDefinitionId.ToString
                    ctlDrop.Skin = ControlSkin
                    ctlDrop.Attributes.Add("ParentId", plhControls.ID)
                    ctlDrop.EnableAjaxSkinRendering = True
                    ctlDrop.Enabled = IsEnabled
                    ctlDrop.OnClientBlur = "UnSelectField"
                    ctlDrop.OnClientFocus = "SelectField"

                    Dim ctlText As New RadTextBox
                    ctlText.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & Constants.ControlId_TextSuffix & ProfileAttribute.PropertyDefinitionId.ToString
                    ctlText.Skin = ControlSkin
                    ctlText.EnableAjaxSkinRendering = True
                    ctlText.Enabled = IsEnabled
                    ctlText.Attributes.Add("ParentId", plhControls.ID)
                    ctlText.ClientEvents.OnBlur = "UnSelectField"
                    ctlText.ClientEvents.OnFocus = "SelectField"

                    Dim drpCountry As RadComboBox = Nothing
                    Dim strCountry As String = ""
                    Dim propCountry As ProfilePropertyDefinition = ProfileController.GetPropertyDefinitionByName(PortalSettings.PortalId, "Country")
                    If Not propCountry Is Nothing Then

                        Dim obj As Control = FindControlRecursive(plhControls, plhControls.ID & "_" & Constants.ControlId_ProfileProperty & propCountry.PropertyDefinitionId.ToString)
                        If Not obj Is Nothing Then
                            drpCountry = CType(obj, RadComboBox)
                            If Not drpCountry Is Nothing Then
                                strCountry = drpCountry.SelectedItem.Text
                            End If
                        End If
                        
                    End If

                    Dim country As Lists.ListEntryInfo = Nothing
                    Dim entries As New Lists.ListEntryInfoCollection
                    Dim lc As New Lists.ListController
                    Dim countries As Lists.ListEntryInfoCollection = lc.GetListEntryInfoCollection("Country")
                    For Each checkCountry As Lists.ListEntryInfo In countries
                        If checkCountry.Text = strCountry Then
                            country = checkCountry
                            Exit For
                        End If
                    Next

                    If Not country Is Nothing Then
                        entries = lc.GetListEntryInfoCollection("Region", "Country." & country.Value)
                    End If

                    Dim blnHasEntries As Boolean = False
                    If Not entries Is Nothing Then
                        If entries.Count > 0 Then
                            blnHasEntries = True
                        End If
                    End If

                    If blnHasEntries Then

                        For Each entry As Lists.ListEntryInfo In entries
                            ctlDrop.Items.Add(New Telerik.Web.UI.RadComboBoxItem(entry.Text, entry.Value))
                        Next
                        Try
                            ctlDrop.SelectedValue = objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName)
                        Catch
                        End Try
                        ctlDrop.Visible = True
                        ctlText.Visible = False

                    Else

                        Try
                            ctlText.Text = Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                        Catch
                        End Try
                        ctlText.Visible = True
                        ctlDrop.Visible = False

                    End If

                    plhControls.Controls.Add(ctlText)
                    plhControls.Controls.Add(ctlDrop)

                Case "locale"
                    Dim ctl As New Telerik.Web.UI.RadComboBox
                    ctl.Skin = ControlSkin
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    Dim locales As Dictionary(Of String, Locale) = LocaleController.Instance.GetLocales(PortalId)
                    For Each entry As Locale In locales.Values
                        ctl.Items.Add(New Telerik.Web.UI.RadComboBoxItem(entry.Text, entry.Code))
                    Next
                    Try
                        ctl.SelectedValue = Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try
                    ctl.Enabled = IsEnabled
                    ctl.EnableAjaxSkinRendering = True
                    ctl.OnClientBlur = "UnSelectField"
                    ctl.OnClientFocus = "SelectField"

                    plhControls.Controls.Add(ctl)

                Case "image"

                    PageNeedsToPostback = True

                    Dim pathVir As String = ""

                    Try
                        System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/"))
                    Catch
                    End Try
                    Try
                        System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/"))
                    Catch
                    End Try


                    If Request.IsAuthenticated Then

                        Try
                            System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/" & objUser.Username & "/"))
                        Catch
                        End Try
                        Try
                            pathVir = PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/" & objUser.Username & "/"
                        Catch
                        End Try


                    Else

                        Try
                            System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/register/"))
                        Catch
                        End Try
                        Try
                            pathVir = PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/register/"
                        Catch
                        End Try


                    End If


                    Dim strExtensions As String() = New String() {".jpg", ".jpeg", ".gif", ".png"}

                    Dim ctl As New RadAsyncUpload
                    ctl.AllowedFileExtensions = strExtensions
                    ctl.EnableFileInputSkinning = True
                    ctl.InitialFileInputsCount = 1
                    ctl.MaxFileInputsCount = 1
                    ctl.EnableAjaxSkinRendering = True
                    ctl.TargetFolder = "~" & pathVir

                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Skin = ControlSkin
                    ctl.ControlObjectsVisibility = ControlObjectsVisibility.None
                    ctl.Enabled = IsEnabled

                    If objUser Is Nothing = False Then

                        Try
                            pathVir = PortalSettings.HomeDirectory & Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                        Catch
                        End Try

                        Dim pathAbs As String = Server.MapPath(pathVir)

                        If System.IO.File.Exists(pathAbs) Then

                            plhControls.Controls.Add(New LiteralControl("<div style=""clear:both;padding-bottom: 10px;"">"))
                            Dim img As New WebControls.Image
                            img.ID = plhControls.ID & "_" & Constants.ControlId_ImageSuffix & ProfileAttribute.PropertyName
                            img.ImageUrl = pathVir
                            img.Width = WebControls.Unit.Pixel(100)
                            plhControls.Controls.Add(img)

                            Dim imgBtn As New ImageButton
                            imgBtn.ImageUrl = ResolveUrl("~/images/delete.gif")
                            imgBtn.ID = plhControls.ID & "_" & Constants.ControlId_ImageRemoveSuffix & ProfileAttribute.PropertyName
                            imgBtn.CommandName = ProfileAttribute.PropertyName
                            imgBtn.ImageAlign = ImageAlign.Right
                            imgBtn.CommandArgument = objUser.UserID.ToString

                            plhControls.Controls.Add(imgBtn)

                            AddHandler imgBtn.Click, AddressOf cmdRemoveFile_Click

                            plhControls.Controls.Add(New LiteralControl("</div>"))

                        End If
                    End If

                    plhControls.Controls.Add(ctl)


                Case "file"

                    PageNeedsToPostback = True

                    Dim pathVir As String = ""

                    Try
                        System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/"))
                    Catch
                    End Try
                    Try
                        System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/"))
                    Catch
                    End Try


                    If Request.IsAuthenticated Then
                        Try
                            System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/" & objUser.Username & "/"))
                        Catch
                        End Try
                        Try
                            pathVir = PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/" & objUser.Username & "/"
                        Catch
                        End Try
                    Else

                        Try
                            System.IO.Directory.CreateDirectory(Server.MapPath(PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/register/"))
                        Catch
                        End Try
                        pathVir = PortalSettings.HomeDirectory & "Profile/" & ProfileAttribute.PropertyName & "/register/"

                    End If

                    Dim strExtensions As String() = New String() {".zip", ".pdf", ".doc", ".txt"}

                    Dim ctl As New RadAsyncUpload
                    ctl.AllowedFileExtensions = strExtensions
                    ctl.EnableFileInputSkinning = True
                    ctl.InitialFileInputsCount = 1
                    ctl.MaxFileInputsCount = 1
                    '                    ctl.OverwriteExistingFiles = True
                    ctl.TargetFolder = pathVir
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Skin = ControlSkin
                    ctl.ControlObjectsVisibility = ControlObjectsVisibility.None
                    ctl.Enabled = IsEnabled
                    ctl.EnableAjaxSkinRendering = True

                    If Not objUser Is Nothing Then

                        pathVir = PortalSettings.HomeDirectory & Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                        Dim pathAbs As String = Server.MapPath(pathVir)

                        If System.IO.File.Exists(pathAbs) Then

                            plhControls.Controls.Add(New LiteralControl("<div style=""clear:both;padding-bottom: 10px;"">"))
                            Dim lnk As New WebControls.HyperLink
                            lnk.NavigateUrl = pathVir
                            lnk.Target = "_blank"
                            lnk.ID = Constants.ControlId_FileSuffix & ProfileAttribute.PropertyName
                            Try
                                lnk.Text = Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                            Catch
                            End Try
                            plhControls.Controls.Add(lnk)

                            Dim imgBtn As New ImageButton
                            imgBtn.ImageUrl = ResolveUrl("~/images/delete.gif")
                            imgBtn.CommandName = ProfileAttribute.PropertyName
                            imgBtn.ImageAlign = ImageAlign.Right                        
                            imgBtn.CommandArgument = objUser.UserID.ToString
                            imgBtn.ID = plhControls.ID & "_" & Constants.ControlId_FileRemoveSuffix & ProfileAttribute.PropertyName

                            plhControls.Controls.Add(imgBtn)

                            AddHandler imgBtn.Click, AddressOf cmdRemoveFile_Click

                            plhControls.Controls.Add(New LiteralControl("</div>"))

                        End If
                    End If

                    plhControls.Controls.Add(ctl)

                Case "richtext"
                    Dim ctl As New RadTextBox
                    ctl.TextMode = InputMode.MultiLine
                    ctl.Rows = 5
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Skin = ControlSkin
                    Try
                        ctl.Text = Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try
                    ctl.Enabled = IsEnabled
                    ctl.EnableAjaxSkinRendering = True
                    ctl.ClientEvents.OnBlur = "UnSelectField"
                    ctl.ClientEvents.OnFocus = "SelectField"
                    ctl.MaxLength = ProfileAttribute.Length
                    plhControls.Controls.Add(ctl)
                Case "timezone"
                    Dim tmp As New DropDownList
                    Localization.LoadTimeZoneDropDownList(tmp, CType(Me.Page, DotNetNuke.Framework.PageBase).PageCulture.Name, Convert.ToString(PortalSettings.TimeZoneOffset))
                    Dim ctl As New Telerik.Web.UI.RadComboBox
                    ctl.Skin = ControlSkin
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    For Each item As ListItem In tmp.Items
                        ctl.Items.Add(New Telerik.Web.UI.RadComboBoxItem(item.Text, item.Value))
                    Next
                    Try
                        ctl.SelectedValue = Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try
                    ctl.EnableAjaxSkinRendering = True
                    ctl.Enabled = IsEnabled
                    ctl.OnClientBlur = "UnSelectField"
                    ctl.OnClientFocus = "SelectField"

                    plhControls.Controls.Add(ctl)
                Case "truefalse", "checkbox"
                    Dim ctl As New CheckBox
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    Try
                        ctl.Checked = Convert.ToBoolean(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try


                    ctl.InputAttributes.Add("onFocus", "SelectCheckboxField(this)")
                    ctl.InputAttributes.Add("onBlur", "UnSelectCheckboxField(this)")

                    plhControls.Controls.Add(ctl)


                Case Else
                    Dim ctl As New RadTextBox
                    ctl.ID = plhControls.ID & "_" & Constants.ControlId_ProfileProperty & ProfileAttribute.PropertyDefinitionId.ToString
                    ctl.Skin = ControlSkin
                    Try
                        ctl.Text = Convert.ToString(objUser.Profile.GetPropertyValue(ProfileAttribute.PropertyName))
                    Catch
                    End Try
                    ctl.EnableAjaxSkinRendering = True
                    ctl.Enabled = IsEnabled
                    ctl.ClientEvents.OnBlur = "UnSelectField"
                    ctl.ClientEvents.OnFocus = "SelectField"
                    ctl.MaxLength = ProfileAttribute.Length
                    plhControls.Controls.Add(ctl)
            End Select

        End Sub

        Public Sub cmdRemoveFile_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

            Dim intUser As Integer = Convert.ToInt32(CType(sender, ImageButton).CommandArgument)
            Dim strProperty As String = Convert.ToString(CType(sender, ImageButton).CommandName)

            Dim objUser As UserInfo = UserController.GetUserById(PortalId, intUser)
            If Not objUser Is Nothing Then

                Dim strPropertyValue As String = objUser.Profile.GetPropertyValue(strProperty)
                If Not strPropertyValue Is Nothing Then
                    If strPropertyValue.Length > 0 Then
                        Dim path As String = Server.MapPath(PortalSettings.HomeDirectory & strPropertyValue)
                        If System.IO.File.Exists(path) Then

                            System.IO.File.Delete(path)
                            objUser.Profile.SetProfileProperty(strProperty, "")
                            UserController.UpdateUser(PortalId, objUser, True)

                        End If
                    End If
                End If

            End If


        End Sub

        Protected Sub drpCountry_SelectedIndexChanged(ByVal o As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)


            Dim drpCountry As RadComboBox = CType(o, RadComboBox)
            Dim objParent As String = drpCountry.Attributes("ParentId")

            Dim strCountry As String = drpCountry.Text

            Dim country As Lists.ListEntryInfo = Nothing
            Dim entries As New Lists.ListEntryInfoCollection
            Dim lc As New Lists.ListController
            Dim countries As Lists.ListEntryInfoCollection = lc.GetListEntryInfoCollection("Country")
            For Each checkCountry As Lists.ListEntryInfo In countries
                If checkCountry.Text = strCountry Then
                    country = checkCountry
                    Exit For
                End If
            Next

            If Not country Is Nothing Then
                entries = lc.GetListEntryInfoCollection("Region", "Country." & country.Value)
            End If

            Dim blnHasEntries As Boolean = False
            If Not entries Is Nothing Then
                If entries.Count > 0 Then
                    blnHasEntries = True
                End If
            End If

            Dim ctlRegionDrop As Control = Nothing
            Dim ctlRegionText As Control = Nothing

            Dim propRegion As ProfilePropertyDefinition = ProfileController.GetPropertyDefinitionByName(PortalSettings.PortalId, "Region")
            If Not propRegion Is Nothing Then

                ctlRegionDrop = FindControlRecursive(Me.Page, objParent & "_" & Constants.ControlId_ProfileProperty & Constants.ControlId_DropdownSuffix & propRegion.PropertyDefinitionId.ToString)
                ctlRegionText = FindControlRecursive(Me.Page, objParent & "_" & Constants.ControlId_ProfileProperty & Constants.ControlId_TextSuffix & propRegion.PropertyDefinitionId.ToString)

            End If

            If blnHasEntries Then
                If Not ctlRegionDrop Is Nothing Then
                    ctlRegionDrop.Visible = True
                    CType(ctlRegionDrop, RadComboBox).Items.Clear()
                    For Each entry As Lists.ListEntryInfo In entries
                        CType(ctlRegionDrop, RadComboBox).Items.Add(New Telerik.Web.UI.RadComboBoxItem(entry.Text, entry.Value))
                    Next
                End If
                If Not ctlRegionText Is Nothing Then
                    ctlRegionText.Visible = False
                End If
            Else
                If Not ctlRegionDrop Is Nothing Then
                    ctlRegionDrop.Visible = False
                End If
                If Not ctlRegionText Is Nothing Then
                    ctlRegionText.Visible = True
                End If
            End If

        End Sub

        Protected Function GetPropertyControl(ByVal ProfProperty As ProfilePropertyDefinition, ByRef objControl As Control) As Control

            Dim strType As String = GetPropertyType(ProfProperty.DataType)
            Select Case strType.ToLower

                Case "region"

                    Dim ctlDrop As Telerik.Web.UI.RadComboBox = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & Constants.ControlId_DropdownSuffix & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctlDrop Is Nothing Then
                        If ctlDrop.Visible Then
                            Return ctlDrop
                        End If
                    End If

                    Dim ctlText As Telerik.Web.UI.RadTextBox = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & Constants.ControlId_TextSuffix & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctlText Is Nothing Then
                        Return ctlText
                    End If

                Case "list", "country", "locale", "timezone"

                    Dim ctl As Telerik.Web.UI.RadComboBox = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

                Case "integer"

                    Dim ctl As Telerik.Web.UI.RadNumericTextBox = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

                Case "richtext"

                    Dim ctl As Telerik.Web.UI.RadTextBox = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

                Case "truefalse"

                    Dim ctl As CheckBox = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

                Case "date"

                    Dim ctl As Telerik.Web.UI.RadDatePicker = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

                Case "datetime"

                    Dim ctl As Telerik.Web.UI.RadDateTimePicker = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

                Case "file", "image"

                    Dim ctl As Telerik.Web.UI.RadUpload = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

                Case Else

                    Dim ctl As Telerik.Web.UI.RadTextBox = objControl.FindControl(objControl.ID & "_" & Constants.ControlId_ProfileProperty & ProfProperty.PropertyDefinitionId.ToString)
                    If Not ctl Is Nothing Then
                        Return ctl
                    End If

            End Select

            Return Nothing

        End Function

        Protected Sub AddSuccessIndicator(ByVal Attribute As String, ByRef objControl As Control)


            Dim pnlRequired As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelRequired & Attribute), Panel)
            If Not pnlRequired Is Nothing Then
                pnlRequired.Visible = False
            End If

            Dim pnlError As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelError & Attribute), Panel)
            If Not pnlError Is Nothing Then
                pnlError.Visible = False
            End If

            Dim pnlSucess As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelSuccess & Attribute), Panel)
            If Not pnlSucess Is Nothing Then
                pnlSucess.Visible = True
            End If


        End Sub

        Protected Sub AddErrorIndicator(ByVal Attribute As String, ByRef objControl As Control)


            Dim pnlRequired As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelRequired & Attribute), Panel)
            If Not pnlRequired Is Nothing Then
                pnlRequired.Visible = False
            End If

            Dim pnlError As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelError & Attribute), Panel)
            If Not pnlError Is Nothing Then
                pnlError.Visible = True
            End If

            Dim pnlSucess As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelSuccess & Attribute), Panel)
            If Not pnlSucess Is Nothing Then
                pnlSucess.Visible = False
            End If

        End Sub

        Protected Sub RemoveErrorIndicator(ByVal Attribute As String, ByRef objControl As Control, ByVal IsRequired As Boolean)

            Dim pnlRequired As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelRequired & Attribute), Panel)
            If Not pnlRequired Is Nothing Then
                pnlRequired.Visible = IsRequired
            End If

            Dim pnlError As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelError & Attribute), Panel)
            If Not pnlError Is Nothing Then
                pnlError.Visible = False
            End If

            Dim pnlSucess As Panel = CType(FindControlRecursive(objControl, objControl.ID & "_" & Constants.ControlId_PanelSuccess & Attribute), Panel)
            If Not pnlSucess Is Nothing Then
                pnlSucess.Visible = False
            End If

        End Sub

        Protected Function GetPropertiesFromTempate(ByVal Template As String) As List(Of String)

            Dim lstProperties As New List(Of String)

            Dim delimStr As String = "[]"
            Dim delimiter As Char() = delimStr.ToCharArray()

            Dim templateArray As String()
            templateArray = Template.Split(delimiter)

            For iPtr As Integer = 0 To templateArray.Length - 1 Step 2


                If iPtr < templateArray.Length - 1 Then

                    Dim strToken As String = templateArray(iPtr + 1)

                    If strToken.StartsWith("CTL:") Then

                        Dim strControlToken As String = ""
                        Try
                            strControlToken = strToken.Substring(4, strToken.Length - 4)
                        Catch
                        End Try

                        Dim strType As String = ""
                        Try
                            strType = strControlToken.Substring(0, strControlToken.IndexOf(":"))
                        Catch
                        End Try

                        Dim strProperty As String = ""
                        Try
                            strProperty = strControlToken.Substring(strControlToken.IndexOf(":") + 1)
                        Catch
                        End Try

                        If strType.ToUpper = "PROFILE" Then

                            lstProperties.Add("P:" & strProperty)

                        ElseIf strType.ToUpper = "USER" Then

                            lstProperties.Add("U:" & strProperty)

                        End If

                    End If

                End If

            Next

            Return lstProperties

        End Function

#End Region

#Region "Localization Utilities"

        Protected Function LocalizeProperty(ByVal prop As ProfilePropertyDefinition) As String
            Dim strText As String = prop.PropertyName
            Try
                strText = Localization.GetString("ProfileProperties_" & prop.PropertyName & ".Text", ProfileResourcefile)
            Catch
            End Try
            Return strText
        End Function

        Protected Function LocalizePropertyHelp(ByVal prop As ProfilePropertyDefinition) As String
            Dim strText As String = prop.PropertyName
            Try
                strText = Localization.GetString("ProfileProperties_" & prop.PropertyName & ".Help", ProfileResourcefile)
            Catch
            End Try
            Return strText
        End Function

        Protected Function LocalizePropertyCategory(ByVal prop As ProfilePropertyDefinition) As String
            Dim strText As String = prop.PropertyCategory
            Try
                strText = Localization.GetString("ProfileProperties_" & prop.PropertyCategory & ".Header", ProfileResourcefile)
            Catch
            End Try
            Return strText
        End Function

#End Region

#Region "Template Utilities"

        Private Function GetTemplatePath(ByVal ViewMode As String, ByVal Templatename As String) As String

            Dim path As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & ViewMode & "/" & Templatename & ".txt")

            If System.IO.File.Exists(path) Then
                Return path
            End If

            Return ""

        End Function

#End Region

    End Class
End Namespace


